/*
 * Copyright 2010    Sylvere RICHARD
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.infotrafficlux.server.dao;

import java.util.Collection;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.google.infotrafficlux.server.bean.Place;

/**
 * @author sly
 * 
 */
@Repository
public class PlaceDaoImpl implements PlaceDao {

	private EntityManager entityManager;

	public EntityManager getEntityManager() {
		return entityManager;
	}

	@PersistenceContext
	@Autowired
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}

	// private Map<Long, Place> map = new HashMap<Long, Place>();
	public PlaceDaoImpl() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.infotrafficlux.server.dao.PlaceDao#getAllPlaces()
	 */
	@Override
	public Collection<Place> getAllPlaces(int index, int count) {

		@SuppressWarnings("unchecked")
		List<Place> places = (List<Place>) getEntityManager()
				.createQuery("SELECT p FROM Place p").setFirstResult(index)
				.setMaxResults(count).getResultList();

		return places;
	}

	@Override
	public Collection<Place> getAllPlaces() {

		@SuppressWarnings("unchecked")
		List<Place> places = (List<Place>) getEntityManager().createQuery(
				"SELECT p FROM Place p").getResultList();

		return places;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.google.infotrafficlux.server.dao.PlaceDao#getPlace(long)
	 */
	@Override
	public Place getPlace(Long id) {
		Place place = getEntityManager().find(Place.class, id);
		return place;
	}

	@Override
	public Long insertOrUpdate(Place p) {
		getEntityManager().persist(p);
		return p.getId();

	}

	@Override
	public void delete(Long id) {
		Place p = getPlace(id);
		if (p == null) {
			return;
		}
		getEntityManager().remove(p);

	}

	@Override
	public Place findByCitaId(String citaId) {
		EntityManager em = getEntityManager();

		try {
			Query query = em
					.createQuery("SELECT b FROM Place b WHERE citaId = :citaId");
			query.setParameter("citaId", citaId);

			return (Place) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		} finally {
			safeClose(em);
		}
	}

	void safeClose(EntityManager em) {
		try {
			em.close();
		} catch (Exception e) { // $codepro.audit.disable emptyCatchClause

		}

	}

}
